package com.burgeon.backend.SHOPBACKEND.service.Impl;


import com.burgeon.backend.SHOPBACKEND.entity.Classify;
import com.burgeon.backend.SHOPBACKEND.entity.ClassifyItem;
import com.burgeon.backend.SHOPBACKEND.mappers.GoodsClassifyItemMapper;
import com.burgeon.backend.SHOPBACKEND.service.GoodsClassifyItemService;
import com.burgeon.backend.service.ex.ErrorDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.burgeon.backend.SHOP.service.Impl.OrderServiceImpl.getOrderIdByUUId;

@Service
public class GoodsClassifyItemServiceImpl implements GoodsClassifyItemService {
    @Autowired
    private GoodsClassifyItemMapper goodsClassifyItemMapper;


    /**
     * 查询商品分类明细
     *
     * @return
     */
    @Override
    public List<Classify> showClassifyIstem(String account) {

        List<Classify> classifies = goodsClassifyItemMapper.getClassify(account);//查询大分类
        for (Classify classify : classifies) {
            classify.setStr(getOrderIdByUUId());
            //根据大分类的id查询小分类
            List<ClassifyItem> classifyItems = goodsClassifyItemMapper.getClaItemByCid(classify.getId());
            for (ClassifyItem classifyItem : classifyItems) {
                classifyItem.setId(classifyItem.getId());//区别大分类处理
                classifyItem.setStr(getOrderIdByUUId());
                int count = goodsClassifyItemMapper.getGoodsCount(classifyItem.getId(), account);//查询的总条数
                classifyItem.setCount(count);
            }
            classify.setClassifyItem(classifyItems);
            int counts = goodsClassifyItemMapper.getGoodsCountByCid(classify.getId(), account);
            classify.setCount(counts);

        }
        return classifies;

    }

    /**
     * 添加商品分类明细
     *
     * @param classifyItem
     * @throws ErrorDataException
     */
    @Override
    public Integer insertClassifyItem(ClassifyItem classifyItem) throws ErrorDataException {
        try {
            return goodsClassifyItemMapper.inClassifyItem(classifyItem);
        } catch (Exception e) {
            throw new ErrorDataException("添加商品分类明细类型错误");
        }

    }

    /**
     * 删除商品分类明细
     *
     * @param id
     * @throws ErrorDataException
     */
    @Override
    public Integer deleteClassifyItem(Integer id) throws ErrorDataException {
        try {
            return goodsClassifyItemMapper.deClassifyItem(id);
        } catch (Exception e) {
            throw new ErrorDataException("删除商品分类明细类型错误");
        }

    }

    @Override
    public Integer upClassifyItemById(ClassifyItem classifyItem) {
        try {
            goodsClassifyItemMapper.upClassifyItemById(classifyItem);
        } catch (Exception e) {
            throw new ErrorDataException("修改商品小类型错误");
        }
        return null;
    }
}
